import {
	base_url
} from './config.js'

class HttpRequest {
	request(url, method, params) { //相当于es5 HttpRequest.prorotype.request = function(){}
		return new Promise((resolse, reject) => {
			uni.request({
				url: base_url + url, //仅为示例，并非真实接口地址。
				data: params,
				header: {
					Authorization: uni.getStorageSync('token')
				},
				method,
				success: (res) => {
					if (res.statusCode == 200) {
						resolse(res.data)
						uni.hideLoading()
					}
				},
				fail: (err) => {
					reject(err);
					uni.hideLoading();
					uni.showToast({
						title: '请求失败',
						duration: 2000
					})
				},
				complete: () => { //不管请求成功或者失败都会执行的函数
					uni.hideLoading();
				}
			});
		})
	}
	get(url, params) {
		return this.request(url, 'GET', params)
	}
	post(url, params) {
		return this.request(url, 'post', params)
	}
}

export default new HttpRequest();
